#include <boost/program_options.hpp>
#include "pickdev.h"
#include "cpfs.h"

using std::string;

void print_usage(char const *argv0)
{
    fprintf(stderr,
"Usage: %s <device|image_file> [SIZE] [OFFSET]\n",
            argv0);
}

int main(int argc, char **argv)
{
    ssize_t blksize;

    namespace po = boost::program_options;
    po::options_description optdesc;
    optdesc.add_options()
            ("offset", "offset to beginning of filesystem")
            ("length", "size of device filesystem is hosted on")
            ("blksize", po::value(&blksize)->default_value(-1), "filesystem sector size")
            ("path", po::value<std::string>(), "device containing the filesystem")
        ;
    po::positional_options_description posopts;
    posopts.add("path", 1);
    po::variables_map varmap;
    po::store(po::command_line_parser(argc, argv).options(optdesc).positional(posopts).run(), varmap);
    po::notify(varmap);
    Device device;
    if (varmap["path"].empty()) {
#       ifdef _WIN32
            if (!choose_device(device))
                return 1;
#       else
            return 1;
#       endif
    } else {
        device.offset = -1;
        device.size = -1;
        device.path = varmap["path"].as<string>();
    }
    if (!varmap["offset"].empty())
        device.offset = varmap["offset"].as<int64_t>();
    if (!varmap["size"].empty())
        device.size = varmap["size"].as<int64_t>();

    if (!cpfs_mkfs(device.path.c_str(), device.size, device.offset, blksize)) {
        fprintf(stderr, "mkfs.cpfs: Error making filesystem\n");
        return 1;
    }
    return 0;
}
